"use strict";

var sockstatus={
	NONE:0,
	CONNECTING:1,
	TIMEOUT:2,
	CONNECTED:3
};

window.uk = {
	game:null,
	sock:null,
	sock_status:sockstatus.NONE,
	sock_timer:null,
	gui:null,
	states:{},
	haslogin:false,
	viewsize:{cx:1024,cy:600},
	cache:{},
	tempdata:{},
	localdata:{},
	uidata:{},
	socklistener:{}
}

uk.uidata.play={};
uk.uidata.play.DESKCARDSWIDTH = 770;
uk.uidata.play.DESKCARDSIZE = {cx:120,cy:160};
uk.uidata.play.DESKCARDSPOS = {x:165,y:184};

uk.uidata.play.HANDCARDSWIDTH = 650;
uk.uidata.play.HANDCARDSSIZE = [{cx:90,cy:120},{cx:75,cy:100}];
uk.uidata.play.HANDCARDSPOS = [{x:202,y:470},{x:202,y:10}];

uk.uidata.play.POOLCARDPOS = [{x:964,y:287},{x:964,y:208}];
uk.uidata.play.POOLCARDSIZE = {cx:60,cy:80};
uk.uidata.play.DEADCARDPOS = [{x:934,y:480},{x:949,y:0}];
uk.uidata.play.DEADCARDSIZE = [{cx:90,cy:120},{cx:75,cy:100}];
uk.uidata.play.FINISHROUNDPOS = {x:912,y:372};

uk.uidata.play.USERINFONAMEPOS=[{x:78,y:228},{x:78,y:228}];
uk.uidata.play.USERINFOHPPOS=[{x:29,y:1},{x:29,y:1}];
uk.uidata.play.USERINFOMPPOS=[{x:29,y:19},{x:29,y:19}];
uk.uidata.play.USERINFOHPMPSIZE={cx:125,cy:17};
uk.uidata.play.USERINFOEXPPOS=[{x:19,y:196},{x:19,y:196}];
uk.uidata.play.USERINFOEXPSIZE={cx:100,cy:15};

uk.uidata.fontstyle={};
uk.uidata.fontstyle.cardname = {font: "20px Arial", fill: "#000000",wordWrap:true,wordWrapWidth:120, align: "center",stroke:'#00cc00',strokeThickness:1};
uk.uidata.fontstyle.remaincardnum = {font: "38px Arial", fill: "#ffffff", align: "center",stroke:'#000000',strokeThickness:3};
uk.uidata.fontstyle.cardsign = {font: "20px Arial", fill: "#ffffff", align: "center",stroke:'#000000',strokeThickness:3};
uk.uidata.fontstyle.stationname = {font: "20px Arial", fill: "#ffffff",wordWrap:true,wordWrapWidth:120, align: "center",stroke:'#000000',strokeThickness:3};
uk.uidata.fontstyle.clocknum = {font: "22px Arial", fill: "#ffffff", align: "center",stroke:'#000000',strokeThickness:3};
uk.uidata.fontstyle.errortip = {font: "28px Arial", fill: "#ffffff", align: "center",stroke:'#ff0000',strokeThickness:3};
uk.uidata.fontstyle.normaltip = {font: "28px Arial", fill: "#ffffff", align: "center",stroke:'#ff0000',strokeThickness:3};
uk.uidata.fontstyle.userinfoname={font: "18px Arial", fill: "#000000", align: "center",weight:'bold'};
uk.uidata.fontstyle.userinfoprogress={font: "14px Arial", fill: "#000000", align: "center",weight:'bold'};

/** guid获取 */
uk.__guidseed = 0xabab;
uk.getnewguid=function(){
    var d = new Date();
	/**
	 * rand(4)-milsec(8)-seed(4)-rand(4);
	 * xxxx
	 *  */
    return uk.rand(1000,10000)+'-'+(d.getTime()&0xfffff)+'-'+((uk.__guidseed++)&0xffff)+'-'+uk.rand(1000,10000);
}
uk.getnewguidn=function(){/**数字型 */
    var d = new Date();
	/**
	 * rand(4)-milsec(8)-seed(4)-rand(4);
	 * xxxx
	 *  */
    return (((d.getTime()&0xffff)<<16)|(((uk.__guidseed++)&0xff)<<8))+uk.rand(0,255);
}

/** 公共方法 */
uk.rand=function(_min,_max){
    return Math.floor(Math.random()*(_max-_min))+_min;
}

/** 获取对象第一层级的属性数 */
uk.countof=function(obj,calcall){
    var _c = 0;
    for(var _k in obj){
        var o = obj[_k];
        var s = typeof o;
        if(s != 'function'){
            _c ++;
			if(calcall){
				_c+=(s=='object')?this.countof(o):0;
			}
        }
    }
    return _c;
}

/** 输出对象到字符串 */
uk.obj2str=function(obj){
    var _c = '';
	if(typeof obj =='object'){
		_c += obj;
		_c += '{';
		var _first=true;
		for(var k in obj){
			if(!_first)_c += ',';
			else _first = false;
			_c += k+':';
			_c += uk.obj2str(obj[k]);
		}
		return _c + '}';
	}else if(typeof obj =='array'){
		_c += '[';
		var _first=true;
		for(var k in obj){
			if(!_first)_c += ',';
			else _first = false;
			_c += uk.obj2str(obj[k]);
		}
		return _c + ']';
	}else{
		return obj;
	}
}

uk.netsend=function(type,obj){
	if(uk.sock == null) return;
	uk.sock.emit(type,obj);
}

/** 绑定网络函数对象 */
uk.netbind = function(type,f,thisobj,insertfront){
	var typelistener = null;
	var registerd = false;
	thisobj = thisobj?thisobj:window;
	if(uk.socklistener.hasOwnProperty(type)){
		typelistener = uk.socklistener[type];
		for(var k in typelistener){
			if(typelistener[k].callback == f && typelistener[k].thisobj==thisobj){
				return true;
			}
		}
		registerd = true;
	}else{
		typelistener = [];
		uk.socklistener[type] = typelistener;
	}
	if(!insertfront){
		typelistener.push({callback:f,thisobj:thisobj});
	}else{
		typelistener.splice(0,0,{callback:f,thisobj:thisobj});
	}
	if(!registerd){
		uk.sock.on(type,function(obj){
			__socket_event_call(type,obj);
		});
	}
}

/** 解绑 */
uk.netunbind=function(type,thisobj){
	if(!uk.socklistener.hasOwnProperty(type))
		return;
	if(this.thisobj == null){
		delete uk.socklistener[type];
		return;
	}else if(type == null){
		for(var t in uk.socklistener){
			var	typelistener = uk.socklistener[t];
			for(var i = 0; i < typelistener.length;){
				if(typelistener[i].thisobj == thisobj){
					typelistener.splice(i,1);
				}else{
					++i;
				}
			}
		}
	}else{
		var	typelistener = uk.socklistener[type];
		for(var i in typelistener){
			if(typelistener[i].thisobj == thisobj){
				typelistener.splice(i,1);
				return;
			}
		}
	}
}

/** 缓存消息 */
uk.netcache_enable=function(enabled){
	console.log('netcache_enable ',enabled);
	uk.socklistener.__cacheenabled = enabled;
	if(!enabled){
		for(var i in uk.socklistener.__cache){
			var ob = uk.socklistener.__cache[i];
			__socket_event_call(ob.type,ob.packet);
		}
	}
	uk.socklistener.__cache = [];
}

function __socket_event_call(type,obj){
	if(uk.socklistener.__cacheenabled){
		uk.socklistener.__cache.push({type:type,packet:obj});
		return;
	}
	var typelistener = uk.socklistener[type];
	for(var i in typelistener){
		var ls = typelistener[i];
		ls.callback.call(ls.thisobj,obj);
	}
}

/** 获取属性名字 */
uk.get_prop_name=function(p){
    switch(p)
    {
        case 'hp':return '生命值';
        case 'mp':return '魔法值';
        case 'def':return '物理防御';
        case 'mdef':return '魔法防御';
        case 'atk':return '攻击力';
        case 'magic':return '魔法力';
        case 'exp':return '经验值';
        case 'lv':return '等级';
        case 'gold':return '金币';
    }
}

/** 连接服务器 */
uk.connect=function(callback,callobj){
	console.log('now status:'+uk.sock_status);
	if(uk.sock_status == sockstatus.CONNECTED){
		if(callback){
			callback.call(callobj,true);
		}
		return;
	}else if(uk.sock_status == sockstatus.CONNECTING){
		return;
	}
	uk.gui.show_tip_message('Connecting');
	uk.sock_status = sockstatus.CONNECTING;
	uk.sock = io.connect('http://127.0.0.1:3000',{timeout:5000,reconnect:false});
	uk.sock.on('connect',function(){
		console.log('连接服务器成功');
		uk.sock_status = sockstatus.CONNECTED;
		uk.gui.show_tip_message(null);
		uk.logic.init();

		if(callback != null)
			callback.call(callobj,true);

		if(uk.sock_timer){
			clearTimeout(uk.sock_timer);
			uk.sock_timer = null;
		}
	});
	uk.sock.on('disconnect',function(){
		console.log('与服务器断开连接');
		uk.gui.show_tip_message(null);
		uk.sock_status = sockstatus.NONE;
		uk.sock.disconnect();
		uk.sock = null;
		if(uk.sock_timer){
			clearTimeout(uk.sock_timer);
			uk.sock_timer = null;
		}
	});
	uk.sock_timer=setTimeout(function(){
		uk.sock_timer = null;
		if(uk.sock_status == sockstatus.CONNECTED){
			return;
		}
		console.log('连接超时');
		uk.sock.disconnect();
		uk.sock = null;
		uk.sock_status = sockstatus.TIMEOUT;
		uk.gui.show_tip_message(null);
		if(callback != null)
			callback.call(callobj,false);
	},10*1000);
}

uk.start_timer=function(timeout,callback,objthis){
    var args=[];
    for(var i = 3;i<arguments.length;++i)
        args.push(arguments[i]);
    return setTimeout(function(){callback.apply(objthis,args);},timeout);
}

uk.stop_timer=function(t){
    clearTimeout(t);
}